package tech.yunjing.train.dao;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;
import tech.yunjing.train.entity.ReceiveRedPacketRecord;

import java.util.Date;
import java.util.List;

/**
 * 抢红包dao
 * Created by zlc on 2017/6/8.
 */
@Repository
public interface ReceiveRedPacketRecordRepository extends JpaRepository<ReceiveRedPacketRecord,String> {

    /**
     * 根据传入的红包记录实体持久化对象
     * @param receiveRedPacketRecord 抢红包记录实体对象
     * @return  红包记录
     */
    ReceiveRedPacketRecord save(ReceiveRedPacketRecord receiveRedPacketRecord);

    /**
     * 根据用户账户和记录状态查询所有未被保存至明细的记录
     * @param userAccount 用户账户
     * @return  该用户所有未保存至明细的记录
     */
    List<ReceiveRedPacketRecord> findByReceiveAccountAndFindState(String userAccount, String findState);

    /**
     * 根据记录Id修改记录查询状态
     * @param findState 状态
     * @param date        更新日期
     * @param receiveRedPacketId 记录id
     * @return 修改成功的记录数
     */
    @Modifying
    @Query(value = "update ReceiveRedPacketRecord set findState = ?1,updateDate = ?2 where receiveRedPacketId = ?3")
    int updateFindState(String findState, Date date, String receiveRedPacketId);

    /**
     * 根据账户和删除状态查询该用户所有未删除记录
     * @param userAccount 用户账户
     * @return 该用户所有未删除记录
     */
    List<ReceiveRedPacketRecord> findByReceiveAccountAndLogicDelete(String userAccount, String logicDelete);

    /**
     * 根据指定账户逻辑删除其所有未删除的抢红包记录
     * @param logicDelete 删除状态
     * @param date        更新日期
     * @param userAccount 用户账户
     * @return  删除的记录数量
     */
    @Modifying
    @Query(value = "update ReceiveRedPacketRecord set logicDelete = ?1,updateDate = ?2 where receiveAccount = ?3 and logicDelete = ?4")
    int logicDelete(String logicDelete, Date date, String userAccount, String logicDelete1);



}
